import { Controller, Post, Body, UseGuards, ParseIntPipe, NotFoundException, BadRequestException } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth, ApiProperty } from '@nestjs/swagger';
import { MenusService } from './menus.service';
import { CreateMenuDto } from './dto/create-menu.dto';
import { UpdateMenuDto } from './dto/update-menu.dto';
import { QueryMenuDto } from './dto/query-menu.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { ApiResponse as ApiResponseDto } from '../common/dto/response.dto';

// 创建查询DTO
class GetMenuDto {
  @ApiProperty({
    description: '菜单ID',
    example: 1,
    required: false
  })
  id?: number;

  @ApiProperty({
    description: '菜单名称',
    example: '用户管理',
    required: false
  })
  name?: string;
}

// 创建删除DTO
class DeleteMenuDto {
  @ApiProperty({
    description: '菜单ID',
    example: 1
  })
  id: number;
}

@ApiTags('菜单管理')
@ApiBearerAuth('JWT-auth')
@Controller('menus')
@UseGuards(JwtAuthGuard)
export class MenusController {
  constructor(private readonly menusService: MenusService) {}

  @Post('create')
  @ApiOperation({
    summary: '创建菜单',
    description: '创建新菜单，支持无限级子菜单'
  })
  @ApiResponse({
    status: 200,
    description: '菜单创建成功'
  })
  @ApiResponse({
    status: 409,
    description: '同级下菜单名称已存在'
  })
  async create(@Body() createMenuDto: CreateMenuDto) {
    const menu = await this.menusService.create(createMenuDto);
    return ApiResponseDto.success(menu, '菜单创建成功');
  }

  @Post('list')
  @ApiOperation({
    summary: '获取菜单列表',
    description: '获取系统中所有菜单列表（平铺结构）'
  })
  @ApiResponse({
    status: 200,
    description: '获取菜单列表成功'
  })
  async findAll() {
    const menus = await this.menusService.findAll();
    return ApiResponseDto.success(menus, '获取菜单列表成功');
  }

  @Post('tree')
  @ApiOperation({
    summary: '获取菜单树',
    description: '获取菜单树形结构，支持无限级子菜单'
  })
  @ApiResponse({
    status: 200,
    description: '获取菜单树成功'
  })
  async findTree() {
    const menuTree = await this.menusService.findTree();
    return ApiResponseDto.success(menuTree, '获取菜单树成功');
  }

  @Post('page')
  @ApiOperation({
    summary: '分页查询菜单',
    description: '支持分页、搜索、筛选的菜单查询'
  })
  @ApiResponse({
    status: 200,
    description: '分页查询成功'
  })
  async findWithPagination(@Body() queryDto: QueryMenuDto) {
    const result = await this.menusService.findWithPagination(queryDto);
    return ApiResponseDto.success(result, '分页查询成功');
  }

  @Post('options')
  @ApiOperation({
    summary: '获取菜单选项',
    description: '获取菜单选项列表，用于下拉选择（仅菜单类型，不包含按钮）'
  })
  @ApiResponse({
    status: 200,
    description: '获取菜单选项成功'
  })
  async getMenuOptions() {
    const options = await this.menusService.getMenuOptions();
    return ApiResponseDto.success(options, '获取菜单选项成功');
  }

  @Post('detail')
  @ApiOperation({
    summary: '获取菜单详情',
    description: '根据菜单ID获取菜单详细信息'
  })
  @ApiResponse({
    status: 200,
    description: '获取菜单信息成功'
  })
  @ApiResponse({
    status: 404,
    description: '菜单不存在'
  })
  async findOne(@Body() getMenuDto: GetMenuDto) {
    if (!getMenuDto.id) {
      throw new BadRequestException('请提供菜单ID');
    }
    
    const menu = await this.menusService.findOne(getMenuDto.id);
    return ApiResponseDto.success(menu, '获取菜单信息成功');
  }

  @Post('update')
  @ApiOperation({
    summary: '更新菜单',
    description: '更新菜单信息，支持修改父菜单（自动重新计算层级）'
  })
  @ApiResponse({
    status: 200,
    description: '菜单更新成功'
  })
  @ApiResponse({
    status: 404,
    description: '菜单不存在'
  })
  @ApiResponse({
    status: 409,
    description: '同级下菜单名称已存在'
  })
  async update(@Body() updateMenuDto: UpdateMenuDto & { id: number }) {
    const { id, ...updateData } = updateMenuDto;
    const menu = await this.menusService.update(id, updateData);
    return ApiResponseDto.success(menu, '菜单更新成功');
  }

  @Post('delete')
  @ApiOperation({
    summary: '删除菜单',
    description: '删除指定菜单（仅当没有子菜单时才能删除）'
  })
  @ApiResponse({
    status: 200,
    description: '菜单删除成功'
  })
  @ApiResponse({
    status: 404,
    description: '菜单不存在'
  })
  @ApiResponse({
    status: 409,
    description: '存在子菜单，无法删除'
  })
  async remove(@Body() deleteMenuDto: DeleteMenuDto) {
    await this.menusService.remove(deleteMenuDto.id);
    return ApiResponseDto.success(null, '菜单删除成功');
  }
}
